<?php
/**
 * 	Bushido
 *
 * @category   Bushido
 * @package    Bushido_Page
 * @copyright  Copyright (c) 2008-2009 Nathan Keyes
 * @author Nathan Keyes
 * @version    $Id: Admin.php 7 2009-11-14 22:42:08Z nkeyes $
 */

/**
 * 
 * @author Nathan Keyes
 *
 */
class Bushido_Page_Admin extends Bushido_Page_Abstract
{
	/**
	 * The name of this page
	 * 
	 * @var string
	 */	protected $pageDisplayName ='Admin';
	
	protected $dbConfig = NULL;
	protected $dbh = NULL;
	
	/**
	 * 
	 * @return string The name of this page
	 */
	public function getPageDisplayName()
	{
		return $this->pageDisplayName;
	}

	
	/**
	 * 
	 * @param $configOptions
	 * @param $view
	 * @return void
	 */
	public function __construct($configOptions = array(), Zend_View $view)
	{	
		$this->_allowedConfigOptions = array('view');
		$this->config = new Bushido_Configurable();
		$this->config->addAllowedConfigOptions($this->_allowedConfigOptions);
		$this->config->setConfigOptions($configOptions);
		
		$this->dbConfig = Bushido_App::session()->dbConfig;
		$this->dbh = Zend_Db::factory($this->dbConfig->databases->my_nisa->Zend_Db);
		$this->_view = $view;

	}
	
	/**
	 * Sets the raw data for this page
	 * @return void
	 */
	protected function _setRawData()
	{
		$configFolder = realpath(APPLICATION_PATH . '/configs');
		$configs = array();
		$contents = scandir($configFolder);
		
		foreach($contents as $fileName)
		{
			$fileName = $configFolder . '/' . $fileName;
			$pathInfo = pathinfo($fileName);
			if(is_file($fileName) /*&& $pathInfo['extension'] === 'xml'*/)
			{
				$configs[$pathInfo['filename']] = new Zend_Config_Xml($fileName);
			}
		}
		
		$this->rawData = array('configs' => $configs);
		$this->rawData['tabs'] = $this->_getTabs($configs);
	}
	
	/**
	 * 
	 * @param $configs
	 * @return string
	 */
	protected function _getTabs($configs)
	{
		$this->_view->tabContainer()->captureStart
		(
			'configFiles', 
		 	array
		 	(
		 	),
		 	array
		 	(
		 		'style' => 'width: 100%; height: 560px;'
		 	)
		);
		foreach($configs as $configName => $configObj)
		{
			$this->_view->tabContainer()->captureStart
			(
				"${configName}Pane", 
				array
				(
					'title' => "$configName",
				),
				array
				(
				)
			);
			echo $this->_getAccordions($configObj, $configName);
			echo $this->_view->tabContainer()->captureEnd("${configName}Pane");
		}

		return $this->_view->tabContainer()->captureEnd('configFiles');
	}
	
	
	
	/**
	 * 
	 * @param $configObj
	 * @param $currentContainer
	 * @return string
	 */
	protected function _getAccordions(Zend_Config $configObj, $currentContainer)
	{
		/*$this->_view->tabContainer()->captureStart
		(
			"${currentContainer}Container", 
		 	array
		 	(
		 	),
		 	array
		 	(
		 		'style' => 'width: 100%; height: 560px;'
		 	)
		);*/
		
		$content = '';
		foreach($configObj as $key => $value)
		{
			if($value instanceof Zend_Config)
			{
				$this->_view->contentPane()->captureStart
				(
					"${currentContainer}_${key}Pane", 
					array
					(
						'title' => "$key",
					),
					array
					(
					)
				);
				echo $this->_getAccordions($value, "${currentContainer}_${key}Pane");
				$content .= $this->_view->contentPane()->captureEnd("${currentContainer}_${key}Pane");

			}
			else
			{
				$content .= "$key:<br />\n<textarea>$value</textarea><br />\n";
			}
			
		}
		return $content;
	}
}